package com.inossem.emergency.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.inossem.emergency.R;
import com.inossem.wms.model.cus.CusRealWh;

import java.util.List;

public class RealWhCodeAdapter extends RecyclerView.Adapter<RealWhCodeAdapter.ViewHolder> {
    private Context context;
    private String chooseCode;
    private List<CusRealWh> list;
    private OnItemListener listener;

    public RealWhCodeAdapter(Context context, String choose, List<CusRealWh> list, OnItemListener listener) {
        this.context = context;
        this.chooseCode = choose;
        this.list = list;
        this.listener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_dialog, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {

        if (list.get(i).getCusWhSys().equals(chooseCode)) {
            viewHolder.layout.setBackgroundColor(ContextCompat.getColor(context, R.color.is_checked));
        } else {
            viewHolder.layout.setBackgroundColor(ContextCompat.getColor(context, R.color.white));
        }
        viewHolder.textView.setText(list.get(i).getCusWhSys() + "-" +list.get(i).getCusWhName());
        viewHolder.layout.setOnClickListener(view -> {
            listener.onItemClickListener(list.get(i).getCusWhSys(), list.get(i).getCusWhName(), false);
        });

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        TextView textView;
        RelativeLayout layout;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.text);
            layout = itemView.findViewById(R.id.layout);
        }
    }

    public interface OnItemListener {
        void onItemClickListener(String typeCode, String typeName, boolean isCellEnabled);
    }

}
